package com.hzit.pool.executor.service.impl;

import com.hzit.pool.executor.dao.ClassesDao;
import com.hzit.pool.executor.dao.StudentDao;
import com.hzit.pool.executor.dto.StudentDto;
import com.hzit.pool.executor.entity.Classes;
import com.hzit.pool.executor.entity.Student;
import com.hzit.pool.executor.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentDao studentDao;
	@Autowired
	private ClassesDao classesDao;
	//1. 引入我们的线程池
	@Autowired
	private ThreadPoolExecutor executor;
	//1. 查询出学生及班级
	@Override
	public StudentDto findAll() throws ExecutionException, InterruptedException {

		//1.1 对两个任务进行异步编排
		StudentDto studentDto = new StudentDto();
		//1.2 第一个异步任务
		CompletableFuture<Void> getStudentsFuture = CompletableFuture.runAsync(() -> {
			studentDto.setStudents(studentDao.selectList(null));
		}, executor);

		//1.3 第二个异步任务
		CompletableFuture<Void> getClassersFuture = CompletableFuture.runAsync(() -> {
			studentDto.setClasses(classesDao.selectList(null));
		}, executor);

		//1.4 多个异步任务同时完成，才会向下执行
		CompletableFuture.allOf(getStudentsFuture,getClassersFuture).get();

		return studentDto;
	}
}
